What is mixpanel?
The Mixpanel npm package is a powerful tool for integrating Mixpanel's analytics and user tracking capabilities into your Node.js applications. It allows you to track events, create user profiles, and send data to Mixpanel for analysis.
What are mixpanel's main functionalities?
Track Events
This feature allows you to track events that occur in your application. You can specify the event name and any properties associated with the event.
const Mixpanel = require('mixpanel');
const mixpanel = Mixpanel.init('YOUR_API_KEY');
mixpanel.track('Event Name', {
distinct_id: 'unique_user_id',
property1: 'value1',
property2: 'value2'
});
Create User Profiles
This feature allows you to create and update user profiles in Mixpanel. You can set various properties for a user, such as their name and email address.
const Mixpanel = require('mixpanel');
const mixpanel = Mixpanel.init('YOUR_API_KEY');
mixpanel.people.set('unique_user_id', {
$first_name: 'John',
$last_name: 'Doe',
$email: 'john.doe@example.com'
});
Increment User Properties
This feature allows you to increment numerical properties of a user profile. For example, you can keep track of how many times a user has logged in.
const Mixpanel = require('mixpanel');
const mixpanel = Mixpanel.init('YOUR_API_KEY');
mixpanel.people.increment('unique_user_id', 'login_count');
Other packages similar to mixpanel
segment
Segment is a customer data platform that helps you collect, clean, and control your customer data. It offers similar event tracking and user profile management capabilities as Mixpanel, but also integrates with a wide range of other analytics and marketing tools.
amplitude
Amplitude is an analytics platform that provides in-depth insights into user behavior. It offers event tracking and user profile management similar to Mixpanel, but is known for its advanced analytics and reporting features.
heap
Heap is an analytics tool that automatically captures all user interactions with your application. It offers similar event tracking and user profile management capabilities as Mixpanel, but focuses on providing a more automated and comprehensive data collection process.
Mixpanel-node
This library provides many of the features in the official JavaScript mixpanel library. It is easy to use, and fully async. It is intended to be used on the server (it is not a client module). The in-browser client library is available
at https://github.com/mixpanel/mixpanel-js.
Installation
npm install mixpanel
Quick Start
var Mixpanel = require('mixpanel');
var mixpanel = Mixpanel.init('<YOUR_TOKEN>');
var mixpanel = Mixpanel.init('<YOUR_TOKEN>', {
protocol: 'https'
});
mixpanel.track('my event', {
distinct_id: 'some unique client id',
as: 'many',
properties: 'as',
you: 'want'
});
mixpanel.track('played_game');
mixpanel.track('my event', {ip: '127.0.0.1'});
mixpanel.track('timed event', {time: new Date()});
mixpanel.people.set('billybob', {
$first_name: 'Billy',
$last_name: 'Bob',
$created: (new Date('jan 1 2013')).toISOString(),
plan: 'premium',
games_played: 1,
points: 0
});
mixpanel.people.set('billybob', {
plan: 'premium',
games_played: 1
}, {
$ignore_time: true
});
mixpanel.people.set('billybob', {
plan: 'premium',
games_played: 1
}, {
$ip: '127.0.0.1'
});
mixpanel.people.set('billybob', 'plan', 'free');
mixpanel.people.set_once('billybob', 'first_game_play', (new Date('jan 1 2013')).toISOString());
mixpanel.people.increment('billybob', 'games_played');
mixpanel.people.increment('billybob', 'points', 15);
mixpanel.people.increment('billybob', {'points': 10, 'games_played': 1});
mixpanel.people.append('billybob', 'awards', 'Great Player');
mixpanel.people.append('billybob', {'awards': 'Great Player', 'levels_finished': 'Level 4'});
mixpanel.people.union('billybob', {'browsers': 'ie'});
mixpanel.people.union('billybob', {'browsers': ['ie', 'chrome']});
mixpanel.people.track_charge('billybob', 39.99);
mixpanel.people.clear_charges('billybob');
mixpanel.people.delete_user('billybob');
mixpanel.people.delete_user('billybob', {$ignore_time: true, $ignore_alias: true});
mixpanel.alias('distinct_id', 'your_alias');
mixpanel.track('test', function(err) { if (err) throw err; });
mixpanel.track_batch([
{
event: 'recent event',
properties: {
time: new Date(),
distinct_id: 'billybob',
gender: 'male'
}
},
{
event: 'another recent event',
properties: {
distinct_id: 'billybob',
color: 'red'
}
}
]);
var mixpanel_importer = Mixpanel.init('valid mixpanel token', {
secret: 'valid api secret for project'
});
mixpanel_importer.track('old event', { gender: '' });
mixpanel_importer.import('old event', new Date(2012, 4, 20, 12, 34, 56), {
distinct_id: 'billybob',
gender: 'male'
});
mixpanel_importer.import_batch([
{
event: 'old event',
properties: {
time: new Date(2012, 4, 20, 12, 34, 56),
distinct_id: 'billybob',
gender: 'male'
}
},
{
event: 'another old event',
properties: {
time: new Date(2012, 4, 21, 11, 33, 55),
distinct_id: 'billybob',
color: 'red'
}
}
]);
FAQ
Where is mixpanel.identify()
?
mixpanel-node
is a server-side library, optimized for stateless shared usage; e.g.,
in a web application, the same mixpanel instance is used across requests for all users.
Rather than setting a distinct_id
through identify()
calls like Mixpanel client-side
libraries (where a single Mixpanel instance is tied to a single user), this library
requires you to pass the distinct_id
with every tracking call. See
https://github.com/mixpanel/mixpanel-node/issues/13.
How do I get or set superproperties?
See the previous answer: the library does not maintain user state internally and so has
no concept of superproperties for individual users. If you wish to preserve properties
for users between requests, you will need to load these properties from a source specific
to your app (e.g., your session store or database) and pass them explicitly with each
tracking call.
Tests
# in the mixpanel directory
npm install
npm test
Alternative Clients and Related Tools
Attribution/Credits
Heavily inspired by the original js library copyright Mixpanel, Inc.
(http://mixpanel.com/)
Copyright (c) 2014-18 Mixpanel
Original Library Copyright (c) 2012-14 Carl Sverre
Contributions from:
License
Released under the MIT license. See file called LICENSE for more
details.